package com.baturu.vin.benz.dal.dao;

import com.baturu.vin.benz.dto.BenzDealRepAndComDTO;
import com.baturu.vin.benz.dto.BenzPartsMappingDTO;
import com.baturu.vin.benz.dto.BenzSelfPartsNameMappingDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author liangguanglong
 * @Date 16/7/13
 * @Time 上午10:56
 */
public interface BenzPartsMappingDAO {

    @Select({"<script>select distinct a.btrPartsName,a.partsCode from benz_parts_mapping as a ",
            "join benz_parts_mapping_ext as b on a.partsCode = b.partsCode"
            ,"where a.btrPartsName in" ,
            "<foreach item='item' index='index' collection='partsNames' open='(' separator=',' close=') '>",
            "#{item}",
            "</foreach>",
            " and b.catNum in ",
            "<foreach item='catNum' index='index' collection='catNums' open='(' separator=',' close=') '>",
            "#{catNum}",
            "</foreach>",
            "</script>"})
    List<BenzPartsMappingDTO> findRelationByBtrPartsName(@Param("partsNames") Set<String> partsName, @Param("catNums") List<String> catNums);

    @Select({"select distinct c.catNum,OPTPART as replacePartsCode,c.btrPartsName,b.groupnum,b.subgrp,b.callout,b.cbpartsCode from bm_parts2_v as b, benz_parts_mapping_tmp as c" ,
            "where b.OPTFLAG='W' AND c.partsCode = b.cbPartsCode" ,
            "and c.catNum = b.CATNUM;"})
    List<BenzDealRepAndComDTO> findCommonPartsRelation();

    @Select({"select distinct c.catNum,CONCAT(trim(c.REPTYPE),trim(c.REPPNO)) as replacePartsCode,c.btrPartsName,b.groupnum,b.subgrp,b.callout,b.cbpartsCode from bm_parts2_v as b, benz_parts_mapping_tmp as c" ,
            "where b.REPLFLG='R' AND c.partsCode = b.cbPartsCode" ,
            "and c.catNum = b.CATNUM;"})
    List<BenzDealRepAndComDTO> findReplacePartsRelation();

    @Insert({"insert IGNORE into benz_parts_mapping_extend_replacecode (CATNUM,btrPartsName,GROUPNUM,SUBGRP,CALLOUT,cbPartsCode,replacePartsCode) values ${insertStr}"})
    int insertNewRelation(@Param("insertStr") String insertString);

    @Select({"<script> select distinct btrPartsName, partsCode, hasImg from benz_self_parts_mapping ",
            "where btrPartsName in ",
            "<foreach item='partsName' index='index' collection='partsNames' open='(' separator=',' close=')' >",
                "#{partsName}",
            "</foreach>",
            "</script>"})
    List<BenzPartsMappingDTO> findRelationByExtraPartsName(@Param("partsNames") Set<String> partsNames);

    @Select({"<script> select distinct btrPartsName, partsCode, hasImg from benz_self_parts_mapping ",
            "where partsCode in ",
            "<foreach item='partsCode' index='index' collection='partsCodes' open='(' separator=',' close=')' >",
            "#{partsCode}",
            "</foreach>",
            "</script>"})
    List<BenzPartsMappingDTO> findRelationByExtraPartsCode(@Param("partsCodes") Set<String> partsCodes);

    @Select({"<script>select distinct partsCode, btrPartsName from benz_parts_mapping",
            "where partsCode in ",
            "<foreach item='item' index='index' collection='partsCodes' open='(' separator=',' close=')' >",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<BenzPartsMappingDTO> getByPartsCode(@Param("partsCodes") Collection<String> partsCodes);

    @Select({"<script> select distinct basic_parts_name basicPartsName, std_parts_name stdPartsName,distinguish_marks distinguishMarks,suffix from benz_btr_parts_mapping ",
            "where status=1 and basic_parts_name in ",
            "<foreach item='btrPartsName' index='index' collection='btrPartsNames' open='(' separator=',' close=')' >",
            "#{btrPartsName}",
            "</foreach>",
            "</script>"})
    Set<BenzSelfPartsNameMappingDTO> findSelfPartsNotOilMapping(@Param("btrPartsNames") Collection<String> btrPartsNames);
}
